package org.wattdepot.cli;

import org.wattdepot.client.BadXmlException;
import org.wattdepot.client.MiscClientException;
import org.wattdepot.client.NotAuthorizedException;
import org.wattdepot.client.ResourceNotFoundException;
import org.wattdepot.client.WattDepotClient;
import org.wattdepot.util.tstamp.Tstamp;

/**
 * Responsible for all power listing functions.
 * 
 * @author Aaron Herres
 * 
 */

public class ListPower implements SingleCommand {

  /**
   * Essentially the main() of the sensor listing class.
   * @author Aaron Herres
   * @param client The WattDepotClient to use in connections.
   * @param args The argument string.
   * @return the output message.
   */
  public String processCommand(WattDepotClient client, String[] args) {
    if (!client.isHealthy()) {
      return Messages.Error.CLIENT_NOT_READY;
    }
    if (args.length < 6) {
      return usageString();
    }
    
    String source = args[3];
    try {
      if (args[2].equalsIgnoreCase("generated")) {
        if (args[4].equalsIgnoreCase("timestamp")) {
          return Double.toString(
              client.getPowerGenerated(source, Tstamp.makeTimestamp(args[5])));
        }
        //generated day
        else {
              return Messages.Error.UNIMPLEMENTED;
        }
      }
      else {
        if (args[4].equalsIgnoreCase("timestamp")) {

          return Double.toString(
              client.getPowerConsumed(source, Tstamp.makeTimestamp(args[5])));
        }
        //consumed day
        else {
              return Messages.Error.UNIMPLEMENTED;
        }
      }
    }
    catch (NotAuthorizedException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    catch (ResourceNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    catch (BadXmlException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    catch (MiscClientException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    return Messages.Error.INVALID_INPUT;
  }
  /**
   * Returns the usage string for this command.
   * @author Aaron Herres
   * @return the usage message.
   */
  @Override
  public String usageString() {
    return "Usage: power [generated|consumed] {source} timestamp {timestamp}"; 
  }
  
}
